//
//  UIImage+Skin.m
//  dyss_v2
//
//  Created by LXF on 16/1/27.
//  Copyright © 2016年 First Facility. All rights reserved.
//

#import "UIImage+Skin.h"
#import "SkinTool.h"
#import <CustomLogTools.h>

#define LocalString(key) NSLocalizedString(key, @"")

NSString *imageName_ex = nil;

@implementation UIImage (Skin)

+(NSString *)xx_imageName_ex{
    if (imageName_ex == nil) {
        imageName_ex = LocalString(@"图片的拼接后缀(_XX)");
    }
    return imageName_ex;
}

+(NSString *)xx_generate_key:(NSString *)key{
    NSString *ex = [self xx_imageName_ex];
    
    if (ex == nil || ex.length == 0) {
        return key;
    }
    
    NSRange range = [key rangeOfString:@"." options:NSBackwardsSearch];
    
    if (range.location != NSNotFound) {
        key = [key stringByReplacingCharactersInRange:range withString:[NSString stringWithFormat:@"%@.",ex]];
    }else{
        key = [key stringByAppendingString:ex];
    }
    
    return key;
}

+(UIImage *)skin:(NSString *)key{
    
    NSString *key2 = [self xx_generate_key:key];
    
    UIImage *image = [SkinTool loadImage:key2];
    
    if (image) {
        return image;
    }
    
    image = [SkinTool loadImage:key];
    image = image ?: [self imageNamed:key];
    if (image == nil) {
        printE(@"图片未找到：%@",key);
    }
    return image;
}

+(UIImage *)skinByL:(NSString *)key{
    
    NSString *key2 = [self xx_generate_key:key];

    UIImage *image = [SkinTool loadImageByLocal:key2];
    
    if (image) {
        return image;
    }
    
    return [SkinTool loadImageByLocal:key];
}

@end
